
//
//  LoginVC.m
//  CustomProject
//
//  Created by 方涛 on 2017/5/10.
//  Copyright © 2017年 puyue.com. All rights reserved.
//

#import "LoginVC.h"
#import "LoginView.h"
#import "RegistVC.h"
#import "ForgetVC.h"
#import "LoginUtils.h"
#import "HomeVC.h"
#import "LoginModel.h"
#import "NSString+UUID.h"
#import "UIButton+CountDown.h"
#import "VersionInformationViewController.h"
//#import <GTSDK/GeTuiSdk.h>     // GetuiSdk头文件应用//
@interface LoginVC ()
@property(nonatomic,strong)LoginView *loginV;
@property(nonatomic,strong)LoginUtils *utils;
@property (nonatomic,strong)  NSString*firstPhone;//发送成功后的手机号，记录下来用于判断是否

@end

@implementation LoginVC
-(LoginUtils *)utils{
    if (!_utils) {
        _utils=[LoginUtils new];
    }
    return _utils;
}
-(LoginView *)loginV{
    if (!_loginV) {
        _loginV=[[LoginView alloc]initWithFrame:self.view.frame];
        kWeakSelf(self)
        [_loginV.registBtn addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            [weakself.navigationController pushViewController:[RegistVC new] animated:YES];
        }];
//        [[_loginV.userNameTF.tf rac_signalForControlEvents: UIControlEventEditingChanged]subscribeNext:^(__kindof UIControl * _Nullable x) {
//            
//            UITextField *textfiedl  = (UITextField*)x;
//            
////            if (![textfiedl.text isEqualToString:self.firstPhone]&&self.firstPhone.length==11)//重置验证码
////            {
////                [_loginV.passWordTF.codeBtn stopTime];
////             [_loginV.passWordTF.codeBtn setTitle:@"发送验证码" forState:0];
////            }
//        }];
        [_loginV.passWordTF.codeBtn addTarget:self action:@selector(SendSMS) forControlEvents:UIControlEventTouchUpInside];
        //忘记密码
        [_loginV.forgetBtn addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            VersionInformationViewController * vc = [VersionInformationViewController new];
            UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
            vc.H5strUrl = [MyControl getObjectForKey:K_reg_article];
            vc.titleName = @"版本信息";
            [weakself presentViewController:nav animated:YES completion:^{
            }];
        }];
        [_loginV.loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginV;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"登录";
    [self.view addSubview: self.loginV];
    [self AddBtn];
    
    [[self.loginV.userNameTF.tf rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *textfiedl  = (UITextField*)x;
        
        if (![textfiedl.text isEqualToString:self.firstPhone]&&self.firstPhone.length==11)//重置验证码
            {
                [self.loginV.passWordTF.codeBtn stopTime];
                [self.loginV.passWordTF.codeBtn DaiJiShiStartTime:0 title:@"重新获取" TitleColor:[UIColor whiteColor] waitTittle:@"秒后重发" WaitTittleColor:[UIColor whiteColor] enableColor:nil startColor:nil];
                
            }
        
    }];

}
-(void)SendSMS
{
    self.utils.cell = self.loginV.userNameTF.tf.text;
    
    [self.utils sendVerifyCodeSucceed:^(id data) {
        
        if ([data[@"Truck"][@"Head"][@"state"] isEqualToString:@"error"])
        {
            [ SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"Truck"][@"Head"][@"msg"]]];
        }
        if ([data[@"Truck"][@"Head"][@"state"] isEqualToString:@"succ"])
        {
            [self.loginV.passWordTF.codeBtn DaiJiShiStartTime:60 title:@"重新获取" TitleColor:[UIColor whiteColor] waitTittle:@"秒后重发" WaitTittleColor:[UIColor whiteColor] enableColor:nil startColor:nil];//发送验证码
            [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@",data[@"Truck"][@"Head"][@"msg"]]];
            self.firstPhone = self.utils.cell;
        }

    } failure:^(NSError *error)
    {
        
    }];
}
/**
 注册会员服务条款
 */
//-(void)forgetBtnClick:(UIButton *)btn {
//    AboutUsViewController * vc = [AboutUsViewController new];
//    vc.H5strUrl = [MyControl getObjectForKey:K_reg_article];
//    vc.titleName = @"服务条款";
//    [self.navigationController pushViewController:vc animated:YES];
//}
-(void)loginBtnClick:(UIButton*)sender
{

    self.utils.cell=self.loginV.userNameTF.tf.text;
    self.utils.checkCode=self.loginV.passWordTF.tf.text;

    if (!self.utils.cell.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
    }
    if (!self.utils.checkCode.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
    }
    if (![MyControl validateMobile:self.utils.cell]) {
        [SVProgressHUD showErrorWithStatus:@"手机号输入有误"];
        return;
    }
    /**
    登录请求
    */
    [sender showIndicator];
    NSString *uuid =  [NSString UUID];
    [MyControl setObject:uuid key:K_UUID];
    [self.utils loginSucceed:^(id data) {
        if ([data[@"Truck"][@"Head"][@"state"] isEqualToString:@"error"]) {
            [ SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"Truck"][@"Head"][@"msg"]]];
            [sender hideIndicator];
        }
        if ([data[@"Truck"][@"Head"][@"state"] isEqualToString:@"succ"]){
            
            [MyControl setObject:self.loginV.userNameTF.tf.text key:K_CELL];
            
            [MyControl setObject:data[@"Truck"][@"Body"][@"user_id"] key:K_USERID];
            [self dismissViewControllerAnimated:YES completion:nil];
            [self.navigationController popViewControllerAnimated:YES];
            LoginModel *model =  [LoginModel mj_objectWithKeyValues:data];
            model.Truck =  [LoginTruck mj_objectWithKeyValues:model.Truck];
            model.Truck.Head = [LoginHead mj_objectWithKeyValues:model.Truck.Head];
            ZFLog(@"q%@",model.Truck.Head.state);
              [sender hideIndicator];
        }

    } failure:^(NSError *error) {
        [sender hideIndicator];
    }];
}
-(void)AddBtn{
    kWeakSelf(self)
    self.navLeftBtn=[UIButton buttonWithFrame:CGRectMake(10, kMainTopHeight-44, 30, 40) withTitle:@"" withTitleColor:nil withSize:15 withImageName:@"icon_fanhui_dl" withBGColor:nil];
    [self.navLeftBtn addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [weakself dismissViewControllerAnimated:YES completion:nil];
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
    
    [self.view addSubview:self.navLeftBtn];
    
}
-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
}
-(void)viewWillDisappear:(BOOL)animated {
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    [super viewWillDisappear:animated];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
